
function sumitSearch(){
	var sql=buildSql()
	//alert(sql);
	if(sql){
		document.all.condition.value=sql;
		document.all.searchform.submit();
	}
	
}

function buildSql(){
	var rows=document.all.searchCondition.rows
	var num=rows.length;
	var row;
	var sql='';
	for(var i=0;i<num;i++){
		row=rows[i];
		if(i==0){
			var colName=row.cells.item(1).children[0].value;
			var colTitle=getColText(colName)
			var colType=getColType(colName);
			var cnd=row.cells.item(1).children[1].value;
			var val=row.cells.item(1).children[2].value;
			if(!val){
				alert('请输入 '+colTitle+' 过滤条件！');
				return false;
				
			}else{
				if('integer'==colType && isNaN(val)){
					alert(colTitle+" 的类型为整型！")
					return false;
					
				}
				
			
			}
			sql+=colName;
			
	
			sql+=" "+cnd+" ";

			if('like'==cnd && !('string'==colType)){
				alert(colTitle+'不能使用检索！');
				return false;
			}else if('like'==cnd && ('string'==colType)){
				sql+=" '%"+val+"%'"
			}else if('string'==colType){
				sql+=" '"+val+"'"
			}else if('date'==colType){
				sql+=" '"+val+""+"'"
			}
			
			else{
				
				sql+=" "+val+""
			}
			
		
			
		}else{
			var lj=row.cells.item(0).children[1].value;
			
			var colName=row.cells.item(1).children[0].value;
			var colTitle=getColText(colName)
			var colType=getColType(colName);
			var cnd=row.cells.item(1).children[1].value;
			var val=row.cells.item(1).children[2].value;
			if(!val)continue;
			sql+=" "+lj+" ";
			sql+=colName;
			sql+=" "+cnd+" ";
			if('like'==cnd && !('string'==colType)){
				alert(colTitle+'不能使用检索！');
				return false;
			}else if('like'==cnd && ('string'==colType)){
				sql+=" '%"+val+"%'"
			}else if('string'==colType || 'date'==colType){
				sql+=" '"+val+"'"
			}else{
				sql+=" "+val+""
			}
			//sql+=" "+val
		}
	}
	return sql;
}

function getColType(colName){
	var selObj=document.all.colNameSel[0];
	
	var opts=selObj.options;
	if(!opts){
		
		selObj=document.all.colNameSel;
		opts=selObj.options;
	}
	var opt,col;
	for(var i=0;i<opts.length;i++){
		opt=opts[i];
		if(opt.value==colName)
			return opt.type;
		//alert('opt.type='+)
	}
}
function getColText(colName){
	var selObj=document.all.colNameSel[0];
	
	var opts=selObj.options;
	if(!opts){
		
		selObj=document.all.colNameSel;
		opts=selObj.options;
	}
	var opt,col;
	for(var i=0;i<opts.length;i++){
		opt=opts[i];
		if(opt.value==colName)
			return opt.text;
		//alert('opt.type='+)
	}
}
function addCondition(){
	var tmpl=document.all.searchCondition.rows[0];
	var newrow=tmpl.cloneNode(true);
	
	
	var targettbl=document.all.searchCondition.appendChild(newrow);
	var str=''
	str+='<input type="checkBox" name="checkFlag">';
	
	str+='<select>'
		str+='<option value="and">并且</option>'
		str+='<option value="or">或者</option>'
	str+='</select>'
	newrow.cells.item(0).innerHTML=str;
}

var prj_row_num;
function delpar(){
	
  var ls_t=document.all("searchCondition");
  var len=ls_t.rows.length;
  var deleteRec=document.all("checkFlag");
  if(deleteRec){
  	try{
  		if(deleteRec && deleteRec.checked) ls_t.deleteRow(1);
  	}catch(e){}
  }else{
  	return;
  }
  var num=deleteRec.length;
  prj_row_num=num;
  
  
  		
 if(num>0)
 {
	for(i=num;i>=-1;i--)
	{
		try{
			if(deleteRec[i-1].checked)
			{
	  			ls_t.deleteRow(i);  
				prj_row_num -= 1;
				if (prj_row_num < 0) prj_row_num = 0;
			}
		}catch(e){}
		
	}
 }

}


function colChange(sel){
		var opts=sel.options;
		var opt;
		if(opts){
			for(var i=0;i<opts.length;i++){
				opt=opts[i];
				if(opt.selected == true){
					if('date'==opt.type){
						//alert(opt.type);
						sel.fun=Calendar.setup(
						    {
						      inputField  : sel.parentElement.children[2],         // ID of the input field
						      ifFormat    : "%Y-%m-%d %H:%M:%S",    // the date format
						     
						      showsTime : true
						    }
						  );
					}else{
						sel.parentElement.children[2].onclick=function(){
							return false;
						}
						if(sel.fun){
							//sel.fun.destroy();
						
						}else{
							//alert('not fun')
						}
					}
				}
			}
		}
		
	}